package br.com.android.pubclub.negocios;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import br.com.android.pubclub.R;
import br.com.android.pubclub.basicas.Estabelecimento;
import br.com.android.pubclub.dados.EstabelecimentoAdapter;
import br.com.android.pubclub.dados.WsServicos;

public class ListaEstabelecimentoAvaliarEstabelecimento extends Activity
		implements OnItemClickListener {

	ProgressDialog dialog;
	private ListView listView;
	ArrayList<Estabelecimento> estabelecimentos;
	EstabelecimentoAssyncTask task;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		dialog = ProgressDialog.show(this, "", "Carregando...", true);
		
		try {
			setContentView(R.layout.activity_listagem);
			estabelecimentos = new ArrayList<Estabelecimento>();
			iniciarDownload();
			listView = (ListView) findViewById(R.id.ListView1);

			ArrayAdapter<Estabelecimento> adapter = new ArrayAdapter<Estabelecimento>(
					this, android.R.layout.simple_list_item_1, estabelecimentos);

			listView.setAdapter(adapter);
			listView.setOnItemClickListener(this);
		} catch (Exception vE) {
			System.out.println(vE.getMessage());
		}

	}

	private void iniciarDownload() {

		ConnectivityManager cm = (ConnectivityManager) this
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		if (cm.getActiveNetworkInfo() != null
				&& cm.getActiveNetworkInfo().isConnected()) {

			task = new EstabelecimentoAssyncTask();
			task.execute();

		} else {
			Toast.makeText(this, "Sem conex‹o com a Internet",
					Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.listagem, menu);
		return true;
	}

	@Override
	public void onItemClick(AdapterView<?> pArg0, View pArg1, int posicao,
			long pArg3) {

		// MapaActivity cdf =
		// MapaActivity.novaInstancia(estabelecimentos.get(posicao));
		Intent it = new Intent(this, WsServicos.class);
		it.putExtra("parametro", "listaEstabelecimentoAvaliar");
		it.putExtra("estabelecimento", estabelecimentos.get(posicao));
		startActivity(it);

	}

	public void refreshList() {
		EstabelecimentoAdapter adapter = new EstabelecimentoAdapter(this,
				estabelecimentos);
		listView.setAdapter(adapter);
	}

	class EstabelecimentoAssyncTask extends
			AsyncTask<Void, Void, ArrayList<Estabelecimento>> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();

		}


		@Override
		protected ArrayList<Estabelecimento> doInBackground(Void... params) {
			try {
				
				Intent it = new Intent(
						ListaEstabelecimentoAvaliarEstabelecimento.this,
						WsServicos.class);
				it.putExtra("parametro", "listaEst");
				startActivity(it);
				return WsServicos.retrieveListaEstabelecimento();
				
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				dialog.dismiss();
			}
			return null;
		}

		@Override
		protected void onPostExecute(ArrayList<Estabelecimento> lista) {
			super.onPostExecute(lista);
			if (lista != null) {
				estabelecimentos = lista;
				refreshList();

			}
		}

	}

}
